package com.hfzy.ihk.web.aliyuncall.controller;

import com.alibaba.fastjson.JSONObject;
import com.hfzy.ihk.common.util.CommonUtils;
import com.hfzy.ihk.common.util.http.HttpClientUtils;
import com.hfzy.ihk.common.util.http.HttpRequester;
import com.hfzy.ihk.common.util.http.HttpRespons;
import com.hfzy.ihk.common.util.uuid.base64uuid.UUIDs;
import com.hfzy.ihk.facade.websocket.linkcall.service.WebSocketService;
import com.hfzy.ihk.web.aliyuncall.biz.AliyunAsrCallOutBiz;
import com.hfzy.ihk.web.aliyuncall.biz.AliyunCallOutBiz;
import com.hfzy.ihk.web.aliyuncall.biz.PushDataToAnalysis;
import com.hfzy.ihk.web.aliyuncall.biz.PushDataToEsBiz;
import com.hfzy.ihk.web.aliyuncall.constant.RedisTimeOut;
import com.hfzy.ihk.web.aliyuncall.vo.AsrCallOutParam;
import com.hfzy.ihk.web.aliyuncall.vo.VoiceRTASRReportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2018/7/18.
 *
 * @author wws
 */
@RestController
@RequestMapping("/test")
public class TestController {


    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    AliyunCallOutBiz aliyunCallOutBiz;

    @Autowired
    PushDataToAnalysis pushDataToAnalysis;

    @Autowired
    AliyunAsrCallOutBiz aliyunAsrCallOutBiz;

    @Autowired
    PushDataToEsBiz pushDataToEsBiz;
//    @Autowired
//    WebSocketService webSocketService;
    @RequestMapping("hi")
    public Map<String,String> getStr(){
//        Map<String,String> map = new HashMap<>();
//        map.put("dd","d");
//        map.put("pp","d");
//        Map<String,String> map1 = new HashMap<>();
//        map1.put("dd","1");
//        map1.put("yy","2");
//        redisTemplate.opsForHash().putAll("testkey",map);
//        redisTemplate.opsForHash().putAll("testkey",map1);
//        Map<String,String> map2 =redisTemplate.opsForHash().entries("testkey");
//        System.out.println("map2:"+map2.toString());
//
//        redisTemplate.opsForSet().add("d","41111");
//        Set<String> set1 = redisTemplate.opsForSet().members("d");
//        System.out.println("set1.size:"+set1.size());
//
//        Set<String> set2 = redisTemplate.opsForSet().members("d2");
//        if(set2==null){
//            System.out.println("我他妈就是空的。");
//        }
//        System.out.println("set2.size:"+set2.size());
//
//      //  boolean d = webSocketService.pushToBrowserByToken("ddd","ddd");
//     //   System.out.println(d);
//        //aliyunCallOutBiz.testPut();
//        return (map2);
        redisTemplate.opsForValue().set("dd","12456", RedisTimeOut.CACHE_ONE_DAY, TimeUnit.SECONDS);
        System.out.println(redisTemplate.opsForValue().get("dd"));
        return null;
    }

    @RequestMapping("myTest")
    public String  test(HttpServletRequest request){
        String jsonData = CommonUtils.getInputStr(request);
        JSONObject jsonObject = JSONObject.parseObject(jsonData);
        System.out.println(jsonObject.getJSONObject("data").getString("data"));
        System.out.println("post 过来的数据："+jsonData);
        return "成功";

    }

    @RequestMapping("myTest2")
    public String  test(){
        VoiceRTASRReportVo voiceRTASRReportVo =new VoiceRTASRReportVo();
        voiceRTASRReportVo.setBegin_offset("11222");
        voiceRTASRReportVo.setEnd_offset("111222");

        voiceRTASRReportVo.setOut_id("dfgdgfdfgdfg");
        voiceRTASRReportVo.setRole("A");
        voiceRTASRReportVo.setWords("你好呀，你好呀125");

        pushDataToAnalysis.pushData("","","",voiceRTASRReportVo,"","");

        try {
            Thread.sleep(100000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "";
    }


    @RequestMapping("AsrCallOut")
    public Map<String,String> AsrCallOut(){
        AsrCallOutParam asrCallOutParam = new AsrCallOutParam();
        asrCallOutParam.setVoiceCode("08a0e68a-9fbe-40b8-a8c5-335af67d89b3.wav");
        asrCallOutParam.setMainCallPhone("13418035053");
        asrCallOutParam.setShowNum("057128204523");
        asrCallOutParam.setExtend(UUIDs.base64UUID());
        return aliyunAsrCallOutBiz.AsrCallOutOpt(asrCallOutParam);
    }



    public static void main(String[] args) {
        HttpRequester httpRequester = new HttpRequester();
        Map<String,String> map = new HashMap<>();
        map.put("dataType","follow");
        map.put("message","{\"data\":{\"id\":75501932,\"customerId\":8872344,\"recommendProperty\":\"保利和光晨樾\",\"lookedProperty\":\"\",\"content\":\"跟老公商量\",\"followUserId\":26467,\"followDate\":1536755883124,\"isTop\":false,\"topUserId\":0,\"regRemindUserId\":26467,\"remindToUserId\":26467,\"remindTime\":1536842296469,\"isViewed\":false,\"relateCloudcallId\":0,\"feedBack\":\"FOLLOWUP\",\"isAppIgnored\":false},\"type\":\"insert\",\"ip\":\"10.10.5.14\",\"date\":\"2018-09-12 20:38:18\"}");
        try {
            HttpRespons respons = httpRequester.sendPost("http://10.10.6.186:8080/crmCustomer/bcData",map);
            System.out.println(respons.getContent());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RequestMapping("testHttp")
    public String testHttp(String str,HttpServletRequest request){
        String jsonData = "";
        try {
             jsonData = pushDataToEsBiz.push("{sessionId:你好}");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jsonData;
    }

    @RequestMapping("testHttp1")
    public String testHttp1() throws Exception{

        String str = HttpClientUtils.postWithJson("http://localhost:50808/aliyuncall/test/testHttp","你好");
        System.out.println(str);
        return str;
    }


}
